﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Gmantis.Controls
{
    public class RelativePopupPresenter : ContentPresenter
    {
        // Fields
        private Size _cachedSize = new Size();
        public static readonly DependencyProperty AutoSizeModeProperty = DependencyProperty.Register("AutoSizeMode", typeof(AutoSizeMode), typeof(RelativePopupPresenter), new PropertyMetadata(AutoSizeMode.GrowAndShrink));

        // Methods
        public void InvalidateAutoSize()
        {
            this._cachedSize = new Size();
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            Size size = base.MeasureOverride(availableSize);
            if (this.AutoSizeMode == AutoSizeMode.GrowOnly)
            {
                double width = Math.Max(this._cachedSize.Width, size.Width);
                this._cachedSize = new Size(width, Math.Max(this._cachedSize.Height, size.Height));
                return this._cachedSize;
            }
            if (this.AutoSizeMode == AutoSizeMode.GrowHorizontally)
            {
                double introduced2 = Math.Max(this._cachedSize.Width, size.Width);
                this._cachedSize = new Size(introduced2, size.Height);
                return this._cachedSize;
            }
            return size;
        }

        // Properties
        public AutoSizeMode AutoSizeMode
        {
            get
            {
                return (AutoSizeMode)base.GetValue(AutoSizeModeProperty);
            }
            set
            {
                base.SetValue(AutoSizeModeProperty, value);
            }
        }
    }
}
